﻿using KYJModels;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KYJDAL
{
    /// <summary>
    /// 用户数据访问类
    /// </summary>
    public class SysAdminService
    {
        public SysAdmin AdminLogin(SysAdmin sysAdmin)
        {
            string sql = "select LoginId,LogAlarm,HistoryLog,ActualTrend,HistoryTrend,ParamSet,Report,UserManage " +
                "from SysAdmin where LoginName=@LoginName and LoginPwd=@LoginPwd";
            SQLiteParameter[] parameters = new SQLiteParameter[]
            {
                new SQLiteParameter("@LoginName", sysAdmin.LoginName),
                new SQLiteParameter("@LoginPwd", sysAdmin.LoginPwd)
            };
            DataSet dataSet = SQLiteHelper.GetDataSet(sql, parameters);
            if (dataSet != null && dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
            {
                sysAdmin.LoginId = Convert.ToInt32(dataSet.Tables[0].Rows[0]["LoginId"]);
                sysAdmin.LogAlarm = Convert.ToBoolean(dataSet.Tables[0].Rows[0]["LogAlarm"]);
                sysAdmin.HistoryLog = Convert.ToBoolean(dataSet.Tables[0].Rows[0]["HistoryLog"]);
                sysAdmin.ActualTrend = Convert.ToBoolean(dataSet.Tables[0].Rows[0]["ActualTrend"]);
                sysAdmin.HistoryTrend = Convert.ToBoolean(dataSet.Tables[0].Rows[0]["HistoryTrend"]);
                sysAdmin.ParamSet = Convert.ToBoolean(dataSet.Tables[0].Rows[0]["ParamSet"]);
                sysAdmin.Report = Convert.ToBoolean(dataSet.Tables[0].Rows[0]["Report"]);
                sysAdmin.UserManage = Convert.ToBoolean(dataSet.Tables[0].Rows[0]["UserManage"]);
                return sysAdmin;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="sysAdmin"></param>
        /// <returns></returns>
        public int AddSysAdmin(SysAdmin sysAdmin)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("insert into SysAdmin(LoginName, LoginPwd, LogAlarm, HistoryLog, ActualTrend, HistoryTrend, ParamSet, Report, UserManage)");

            stringBuilder.Append(" values(@LoginName, @LoginPwd, @LogAlarm, @HistoryLog, @ActualTrend, @HistoryTrend, @ParamSet, @Report, @UserManage)");

            SQLiteParameter[] parameter = new SQLiteParameter[]
                {
                    new SQLiteParameter("@LoginName",sysAdmin.LoginName),
                    new SQLiteParameter("@LoginPwd",sysAdmin.LoginPwd),
                    new SQLiteParameter("@LogAlarm",sysAdmin.LogAlarm),
                    new SQLiteParameter("@HistoryLog",sysAdmin.HistoryLog),
                    new SQLiteParameter("@ActualTrend",sysAdmin.ActualTrend),
                    new SQLiteParameter("@HistoryTrend",sysAdmin.HistoryTrend),
                    new SQLiteParameter("@ParamSet",sysAdmin.ParamSet),
                    new SQLiteParameter("@Report",sysAdmin.Report),
                    new SQLiteParameter("@UserManage",sysAdmin.UserManage)
                };
            return SQLiteHelper.ExecuteNonQuery(stringBuilder.ToString(), parameter);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public int DeleteSysAdmin(int loginId)
        {
            string sql = "delete from SysAdmin where LoginId=@LoginId";
            SQLiteParameter[] param = new SQLiteParameter[]
            {
                new  SQLiteParameter("@LoginId",loginId)
            };
            return SQLiteHelper.ExecuteNonQuery(sql, param);
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="sysAdmin"></param>
        /// <returns></returns>
        public int ModifySysAdmin(SysAdmin sysAdmin)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("update SysAdmin set LoginName=@LoginName, LoginPwd=@LoginPwd, LogAlarm=@LogAlarm,");

            stringBuilder.Append("HistoryLog=@HistoryLog, ActualTrend=@ActualTrend, HistoryTrend=@HistoryTrend,");

            stringBuilder.Append("ParamSet=@ParamSet, Report=@Report, UserManage=@UserManage Where LoginId=@LoginId");

            SQLiteParameter[] parameter = new SQLiteParameter[]
              {
                    new SQLiteParameter("@LoginName",sysAdmin.LoginName),
                    new SQLiteParameter("@LoginPwd",sysAdmin.LoginPwd),
                    new SQLiteParameter("@LogAlarm",sysAdmin.LogAlarm),
                    new SQLiteParameter("@HistoryLog",sysAdmin.HistoryLog),
                    new SQLiteParameter("@ActualTrend",sysAdmin.ActualTrend),
                    new SQLiteParameter("@HistoryTrend",sysAdmin.HistoryTrend),
                    new SQLiteParameter("@ParamSet",sysAdmin.ParamSet),
                    new SQLiteParameter("@Report",sysAdmin.Report),
                    new SQLiteParameter("@UserManage",sysAdmin.UserManage),
                    new SQLiteParameter("@LoginId",sysAdmin.LoginId)
              };
            return SQLiteHelper.ExecuteNonQuery(stringBuilder.ToString(), parameter);
        }

        /// <summary>
        /// 查询用户
        /// </summary>
        /// <returns></returns>
        public List<SysAdmin> QuerySysAdmins()
        {
            string sql = "select LoginId,LoginName,LoginPwd,LogAlarm,HistoryLog,ActualTrend,HistoryTrend,ParamSet,Report,UserManage from SysAdmin ";

            SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql);

            List<SysAdmin> sysAdmins = new List<SysAdmin>();

            while (reader.Read())
            {
                sysAdmins.Add(new SysAdmin()
                {
                    LoginId = Convert.ToInt32(reader["LoginId"]),
                    LoginName = reader["LoginName"].ToString(),
                    LoginPwd = reader["LoginPwd"].ToString(),
                    LogAlarm = Convert.ToBoolean(reader["LogAlarm"]),
                    HistoryLog = Convert.ToBoolean(reader["HistoryLog"]),
                    ActualTrend = Convert.ToBoolean(reader["ActualTrend"]),
                    HistoryTrend = Convert.ToBoolean(reader["HistoryTrend"]),
                    ParamSet = Convert.ToBoolean(reader["ParamSet"]),
                    Report = Convert.ToBoolean(reader["Report"]),
                    UserManage = Convert.ToBoolean(reader["UserManage"])
                });
            }
            reader.Close();
            return sysAdmins;
        }
    }
}
